<template>
  <el-dialog v-bind="$attrs" title="商品退款" :visible.sync="visible" width="600px">
      <el-alert
        class="mb-20"
        :closable="false"
        title="本次发起退款，将修改未登记退款的金额，若退款已全部登记，则新增一条退款记录。"
        type="info"
        show-icon>
      </el-alert>
    <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="100px">
      <el-form-item label="商品信息" v-if="showGoods">
        <el-tag effect="plain" class="mr-5" size="mini">{{detail.categoryName}}</el-tag>
        <span>
          {{ detail.goodsName }}
          (规格：{{ detail.goodsSpecs }})
        </span>
        <agianRefundInfo :detail="detail"></agianRefundInfo>
      </el-form-item>
      <el-form-item label="退款金额" prop="price">
        <el-input
          v-model="form.price"
          placeholder="请输入金额"
          @input="form.price=form.price.replace(/[^\d.]/g,'')"
          maxlength="10"
          show-word-limit
          class="w-200"
        ></el-input>
      </el-form-item>
      <el-form-item label="退款原因" prop="reasonRefund">
        <el-input
          type="textarea"
          v-model="form.reasonRefund"
          placeholder="请输入退款原因"
          maxlength="500"
          show-word-limit
          rows="5"
        ></el-input>
      </el-form-item>
      <el-form-item label="退款二维码">
        <XmUpload
          ref="xmupload"
          :data="{ tag: 'good' }"
          :limit="1"
          :on-exceed="handleExceed"
          :on-success="handleAvatarSuccess"
        >
        </XmUpload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button @click="$refs.orderLogDialog.show({orderId: order.id})">日 志</el-button>
      <el-button type="primary" @click="onCofirm">确 定</el-button>
    </div>
    <orderLogDialog ref="orderLogDialog"></orderLogDialog>
  </el-dialog>
</template>

<script>
import { updateRefundPrice } from '@/api/pending'
import orderLogDialog from '@/components/dialog/orderLogDialog'
import agianRefundInfo from './agianRefundInfo';

export default {
  props: {
    showGoods: {
      type: Boolean,
      default: true
    }
  },
  components: {
    orderLogDialog,
    agianRefundInfo
  },
  data() {
    return {
      visible: false,
      form: {
        type: '',// 0退款，1退款不退货
        price: '',
        reasonRefund: '',
        refundCode: '',
      },
      order: {},
      detail: {},
      rules: {
        price: [
          { required: true, message: "请输入退款金额", trigger: "blur" },
        ],
        reasonRefund: [
          { required: true, message: "请输入退款原因", trigger: "blur" },
        ],
      }
    };
  },
  methods: {
    show({detail, order, type} = {}) {
      this.resetForm()
      this.detail = detail;
      this.order = order;
      this.visible = true;
      this.form.type = type
    },
    resetForm() {
      this.form.price = ''
      this.form.reasonRefund = ''
      this.form.refundCode = ''
      this.$refs.xmupload && this.$refs.xmupload.clearFiles()
    },
    onCofirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const param = {
            type: this.form.type,
            refundPrice: this.form.price,
            reasonRefund: this.form.reasonRefund,
            workOrderRefundId: this.detail.workRefundId,
            refundCode: this.form.refundCode || undefined,
          };
          updateRefundPrice(param).then(res => {
            if (res.data.code == 200) {
              this.visible = false;
              this.confirmDialog({ msg: `再次发起退款成功，详情请查看订单日志，后台单号：${this.order.xmbzNo}` });
              this.$emit('confirm')
            }
            else {
              this.$message({
                type: 'error',
                message: res.data.msg
              });
            }
          });
        }
      })
    },
    // 提交后提示单号弹窗
    confirmDialog({ msg }) {
      this.$confirm(msg, '提示', {
        showCancelButton: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      });
    },
    // 主图开始上传
    handleAvatarSuccess(response, file, fileList) {
      this.form.refundCode = fileList[fileList.length - 1].response.data;
    },
    // 文件超出数量
    handleExceed() {
      this.$message({
        type: 'warning',
        message: '只能上传一张图片'
      })
    }
  },
}
</script>

<style lang="scss" scoped></style>
